#include "clib_stack_conf.h"
#include "clib_config.h"

/** TODO 默认配置 **/
stack_conf_t  stack_cfg = {
    .sack = 0,
    .sack_perm = 0,
    .wack = 0,
    .scale = 0,
    .timestamp = 0,
    .hand_ack = 0, 
    .close = 0,   
    .lock = 0,
    .mtu = 1460,
    .recv_buf = 8192,
    .send_buf = 8192,
    .route = 0,
    .arp = 0,
    .ndp = 0,
    .time_wait = 10,
    .time_out  = 2,
    .conn = 0,
    .sock_cnt = 0,
    .thd_cnt  = 0
};

static i32_t 
stack_conf_set(clib_config_t *conf)
{
    if(!conf) {
        return -1;
    }

    if(!strcmp(conf->key,"sack")) {
        if(!strcmp("yes",conf->value.ptr)) {
            stack_cfg.sack = 1;
        } else {
            stack_cfg.sack = 0;
        }
         
    } else if(strcmp(conf->key,"sack_perm")) {
        if(!strcmp("yes",conf->value.ptr)) {
            stack_cfg.sack_perm = 1;
        } else {
            stack_cfg.sack_perm = 0;
        }
    } else if(strcmp(conf->key,"wack")) {
        stack_cfg.wack = conf->value.u64;
    } else if(strcmp(conf->key,"scale")) {
        if(!strcmp("yes",conf->value.ptr)) {
            stack_cfg.scale = 1;
        } else {
            stack_cfg.scale = 0;
        }
    } else if(strcmp(conf->key,"timestamp")) {
        stack_cfg.timestamp = conf->value.u64;
    } else if(strcmp(conf->key,"hand_ack")) {
        stack_cfg.hand_ack = conf->value.u64;
    } else if(strcmp(conf->key,"close")) {
        if(!strcmp("fin",conf->value.ptr)) {
            stack_cfg.close = 0;
        } else if(!strcmp("rst",conf->value.ptr)) {
            stack_cfg.scale = 1;
        }
    } else if(strcmp(conf->key,"lock")) {
        stack_cfg.lock = conf->value.u64;
    } else if(strcmp(conf->key,"mtu")) {
        stack_cfg.mtu = conf->value.u64;
    } else if(strcmp(conf->key,"recv_buf")) {
        stack_cfg.recv_buf = conf->value.u64;
    } else if(strcmp(conf->key,"send_buf")) {
        stack_cfg.recv_buf = conf->value.u64;
    } else if(strcmp(conf->key,"route")) {
        stack_cfg.route = conf->value.u64;
    } else if(strcmp(conf->key,"arp")) {
        stack_cfg.arp = conf->value.u64;
    } else if(strcmp(conf->key,"ndp")) {
        stack_cfg.ndp = conf->value.u64;
    } else if(strcmp(conf->key,"time_wait")) {
        stack_cfg.time_wait = conf->value.u64;
    } else if(strcmp(conf->key,"time_out")) {
        stack_cfg.time_out = conf->value.u64;
    } else if(strcmp(conf->key,"conn")) {

    } else if(strcmp(conf->key,"sock_cnt")) {
        stack_cfg.sock_cnt = conf->value.u64;
    } else if(strcmp(conf->key,"thread")) {
        stack_cfg.thd_cnt = conf->value.u64;
    }
}

i32_t
clib_stack_read_conf(const i08_t* file)
{
    clib_config_t cfg;
    clib_config_t *item;
    
    clib_config_init(&cfg);
    if(clib_config_read(&cfg,file) < 0) {
        return -1;
    }

    item = clib_config_first_child(&cfg);

    for(; item != NULL; item) {
        if(item->type == clib_config_type_str && !item->value.ptr) {
            continue;
        }
        stack_conf_set(item);
    }
    return 0;
}